﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FHost1.Admin
{
    public partial class EditPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int pid = 0;
                if (Request.QueryString.AllKeys.Contains("pid"))
                {
                    if (Request.QueryString["pid"] != "")
                    {
                        pid = Convert.ToInt32(Request.QueryString["pid"]);
                    }

                    if (pid > 0)
                    {
                        GetPageDetails(pid);
                    }
                }
            }
        }

        private void GetPageDetails(int pid)
        {
            using (FHostDataContext dataContext = new FHostDataContext())
            {
                var result = dataContext.SitePages.Where(obj => (obj.PageID == pid));
                foreach (var item in result)
                {
                    txtTitle.Text = item.Title;
                    txtUrlCode.Text = item.UrlCode;
                    contentEditor.Text = item.Content;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            int pid = 0;
            if (Request.QueryString.AllKeys.Contains("pid"))
            {
                if (Request.QueryString["pid"] != "")
                {
                    pid = Convert.ToInt32(Request.QueryString["pid"]);
                }
            }

            using (FHostDataContext dataContext = new FHostDataContext())
            {
                dataContext.UpdatePage(pid, contentEditor.Text);
            }
            Response.Redirect("~/Admin/Pages.aspx");
        }
    }
}